package com.ycdr.leetcode.stack.exercise;

import com.ycdr.leetcode.stack.example.StackX;

/**
 * 分隔符匹配
 * 检查分隔符是否满足左右匹配
 * 如 {1{2}}[1(2)] 匹配
 *    {1{2}[1】】
 */
public class DelimiterMatching {
    public static void main(String[] args) {
        StackX x=new StackX(20);
        String s="{1{2}}[1(2))]";
        char[] dd = s.toCharArray();
        for (int i = 0; i < dd.length; i++) {
            match(x,Character.toString(dd[i]));
        }
    }

    public static void match(StackX x,String s){
        if ("{".equals(s)||"[".equals(s)||"(".equals(s)){
            x.push(s);
        }
        if ("}".equals(s)){
            if (!x.pop().equals("{")){
                throw new RuntimeException("不匹配");
            }
        }
        if ("]".equals(s)){
            if (!x.pop().equals("[")){
                throw new RuntimeException("不匹配");
            }
        }
        if (")".equals(s)){
            if (!x.pop().equals("(")){
                throw new RuntimeException("不匹配");
            }
        }

    }
}
